<?php
	class MailManageAction extends Action{
		//群发站内信
		public function mail(){
			$this->display();
		}
		//处理群发站内信
		function do_mail(){	
			$data['Title']=$this->_post('Title');			//获取标题
			$data['Content']=$this->_post('description');	//获取内容
			$data['IsRead']=0;								//标明未读状态
			$data['CreateDate']=date('Y-m-d H:i:s',time());	//获取时间
			$term=$this->_post('term');						//获取判断值
			$data['SenderName']=$_SESSION['TrueName'];
			$data['Sender']=$_SESSION['UserId'];
			//如果是全部执行
			if($term==0){
				$user=M('users');				//实例化用户表						
				$arr=$user->field('UserName,UserId')->select();	//获取评委信息
				$mail=M('Mail');				//实例化邮件表
				foreach($arr as $v){					
					$data['Receiverid']=$v['UserId'];		//发送者id
					$data['Receiver']=$v['UserName'];		//发送者姓名
					$res=$mail->add($data);					//插入数据库
				}
			}
			//如果是学生执行
			if($term==1){
				$user=M('users');				//实例化用户表						
				$arr=$user->field('UserName,UserId')->where('UserType=2')->select();	//获取学生信息
				$mail=M('Mail');				//实例化邮件表
				foreach($arr as $v){					
					$data['Receiverid']=$v['UserId'];		//发送者id
					$data['Receiver']=$v['UserName'];		//发送者姓名
					$res=$mail->add($data);					//插入数据库
				}	
			}
			//如果是老师执行
			if($term==2){
				$user=M('users');				//实例化用户表						
				$arr=$user->field('UserName,UserId')->where('UserType=3')->select();	//获取教师信息
				$mail=M('Mail');				//实例化邮件表
				foreach($arr as $v){					
					$data['Receiverid']=$v['UserId'];		//发送者id
					$data['Receiver']=$v['UserName'];		//发送者姓名
					$res=$mail->add($data);					//插入数据库
				}	
			}
			//如果是教官执行
			if($term==3){
				$user=M('users');				//实例化用户表						
				$arr=$user->field('UserName,UserId')->where('UserType=4')->select();	//获取企业教官信息
				$mail=M('Mail');				//实例化邮件表
				foreach($arr as $v){					
					$data['Receiverid']=$v['UserId'];		//发送者id
					$data['Receiver']=$v['UserName'];		//发送者姓名
					$res=$mail->add($data);					//插入数据库
				}	
			}
			//如果是评委执行
			if($term==4){
				$user=M('users');				//实例化用户表						
				$arr=$user->field('UserName,UserId')->where('IsJury=1')->select();	//获取评委信息
				$mail=M('Mail');				//实例化邮件表
				foreach($arr as $v){					
					$data['Receiverid']=$v['UserId'];		//发送者id
					$data['Receiver']=$v['UserName'];		//发送者姓名
					$res=$mail->add($data);					//插入数据库
				}	
			}
			//如果是管理员执行
			if($term==5){
				$user=M('users');				//实例化用户表						
				$arr=$user->field('UserName,UserId')->where('UserType=6')->select();	//获取评委信息
				$mail=M('Mail');				//实例化邮件表
				foreach($arr as $v){					
					$data['Receiverid']=$v['UserId'];		//发送者id
					$data['Receiver']=$v['UserName'];		//发送者姓名
					$res=$mail->add($data);					//插入数据库
				}		
			}
			//如果是文章编辑者执行
			if($term==6){
				$user=M('users');				//实例化用户表						
				$arr=$user->field('UserName,UserId')->where('UserType=7')->select();	//获取评委信息
				$mail=M('Mail');				//实例化邮件表
				foreach($arr as $v){					
					$data['Receiverid']=$v['UserId'];		//发送者id
					$data['Receiver']=$v['UserName'];		//发送者姓名
					$res=$mail->add($data);					//插入数据库
				}
			}
			if($res){
				$this->success_ajax('发送成功');
			}else{
				$this->error_ajax('发送失败');
			}
		}
		public function time(){
			$time=M('time');
			$list=$time->find();
			$this->assign('list',$list);
			$this->display();
		}
		public function do_time(){
			$id=$this->_post('tid');
			$time=M('time');
			$time->create();
			if(!$time->where('Id='.$id)->select()){
				$res=$time->add();
			}else{
				$res=$time->where('Id='.$id)->save();
			}
			if($res){
				$this->success_ajax('成功');
			}else{
				$this->error_ajax('失败');
			}
		}
	}	

